package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import com.qk.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;


    //角色列表查询
    @GetMapping
    public Result page(String name,String label,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询参数:name:{},label:{},page:{},pageSize:{}" , name,label, page, pageSize);
        PageResult<Role> pageResult = roleService.list(name,label, page, pageSize);
        return Result.success(pageResult);
    }
    //根据ID删除角色
    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除角色id:{}" , id);
        roleService.deleteById(id);
        return Result.success();
    }
    //新增角色
    @Log
    @PostMapping
    public Result add(@RequestBody Role role){
        log.info("新增角色:{}" , role);
        return roleService.add(role);
    }

    //根据ID查询角色
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询角色id:{}" , id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Role role){
        log.info("修改角色:{}" , role);
        roleService.update(role);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(String name, String label, LocalDateTime createTime, LocalDateTime updateTime){
        log.info("查询参数:name:{},label:{},createTime:{},updateTime:{}" , name,label,createTime,updateTime);
        return roleService.allList(name,label,createTime,updateTime);
    }

}
